// pages/user/hongbao.js
const app = getApp()
var util = require('../../utils/util.js')
var http = require('../../utils/server.js')
Page({
  data: {
    list: [],
    open: false,
    openData: null,
    showCoin: false
  },
  onLoad: function(options) {
    var that = this;
    var ui = wx.getStorageSync("userInfo");
    that.setData({
      userInfo: ui
    })
    //筛选条件
    http.post({
      url: "Redpacket/getList"
    }, function(data) {
      console.log('红包列表', data)
      that.setData({
        list: data
      })
    })
  },
  open: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.list[index];

    //开单个红包
    http.post({
      url: "Redpacket/open",
      id: item.id,
    }, function(data) {
      if (data.user_id) {
        that.setData({
          showCoin: true
        })
        setTimeout(function() {
          const innerAudioContext = wx.createInnerAudioContext()
          innerAudioContext.autoplay = true
          innerAudioContext.obeyMuteSwitch = false
          innerAudioContext.src = 'https://www.ymqvip.com/static/music/coin.mp3'
          console.log('打开红包', data);
          that.data.list.splice(index, 1);
          that.setData({
            list: that.data.list,
            open: true,
            showCoin: false,
            openData: item
          })
          setTimeout(function() {
            that.setData({
              animation: true
            })
          }, 10)
        }, 1000)
      } else {
        that.setData({
          showCoin: false
        })
      }
    })
  },
  allOpen: function(e) {
    var that = this
    var list = that.data.list
    var ids = []
    for (var i in list) {
      ids.push(list[i].id)
    }
    //开全部红包
    http.post({
      url: "Redpacket/allOpen",
      ids: ids,
    }, function(data) {
      if (data.total > 0) {
        that.setData({
          showCoin: true
        })
        setTimeout(function() {
          const innerAudioContext = wx.createInnerAudioContext()
          innerAudioContext.autoplay = true
          innerAudioContext.obeyMuteSwitch = false
          innerAudioContext.src = 'https://www.ymqvip.com/static/music/coin.mp3'
          console.log('打开红包', data);
          that.setData({
            list: that.data.list,
            open: true,
            showCoin: false,
            openData: {
              money: data.total,
              user: {
                avatar: that.data.userInfo.avatar,
                name: that.data.userInfo.name
              }
            }
          })
          setTimeout(function() {
            that.setData({
              animation: true
            })
          }, 10)
        }, 1000)
      } else {
        util.showError('没有红包可拆了~~~')
        that.setData({
          showCoin: false
        })
      }
    })
  },
  close: function() {
    this.setData({
      open: false
    });
    this.onLoad()
  },
  torecord: function() {
    wx.navigateTo({
      url: '/pages/user/hongbaorecord',
    })
  },
  goStrategy: function() {
    wx.navigateTo({
      url: '/pages/strategy/strategy',
    })
  }
})